package com.supertimetable.tools;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;

import com.supertimetable.bean.Teacher;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;




public class HTMLUtils {


    public static ArrayList<Teacher> getTeachers(){
        ArrayList<Teacher> teachers = null;

        try {
//            String path = "http://qg.peizheng.net.cn/ZNPK/Private/List_JS.aspx?xnxq=20170&js=";
            String path = "http://jwgl.ncist.edu.cn/ZNPK/Private/List_JS.aspx?xnxq=20170&js=";
//            String cookie = "ASP.NET_SessionId=humciw554ebnzrazkwnn3i55";
//            String referer = "http://qg.peizheng.net.cn/ZNPK/TeacherKBFB.aspx";
            String referer = "http://jwgl.ncist.edu.cn/ZNPK/TeacherKBFB.aspx";
            String params = " ";

            StringBuffer sb;

            sb = HTMLUtils.getHTML(path, null, referer, params);
            teachers = (ArrayList<Teacher>)AnalysisUtils.getTeachers(sb);
            System.out.println("size------------------------------"+teachers.size());
//            Log.i("size------------------------------", teachers.size() + "");
        } catch (Exception e) {
            e.printStackTrace();
        }

        return teachers;
    }

    //将HttpURLConnection 转化为StringBuffer
    public static StringBuffer getStringBufferFromNet(HttpURLConnection connection) throws Exception {
        InputStream input = connection.getInputStream();
        byte[] buf = new byte[1024];
        int len=0;
        StringBuffer buffer=new StringBuffer();
        while((len= input.read(buf))!=-1){
            String html = new String(buf, 0, len, "gbk");
            buffer.append(html);
        }
        System.out.println(buffer.toString());
        input.close();
        return buffer;

    }


    //将File 对象转化为StringBuffer
    public static StringBuffer Html_Buffer(File file) throws Exception {
        String html = null;
        InputStream in = null;
        in = new FileInputStream(file);
        byte[] bytes = new byte[1024];
        int len = 0;
        StringBuffer buffer=new StringBuffer();
        while((len= in.read(bytes))!=-1){
            html = new String(bytes, 0, len, "gbk");
            buffer.append(html);
        }
        in.close();
        return buffer;

    }



    //从本地获得Html StringBuffer
    public static StringBuffer getStringBufferFromHTML(){
        StringBuffer sb = null;
        String html = null;
        InputStream in = null;
        File file = new File("sdcard/html/teacher.html");
        try {
            sb = HTMLUtils.Html_Buffer(file);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return sb;
    }

    //获取验证码bitmap
    public static Bitmap getVCodeBitBitmap(String path, String cookie,String referer){
        byte[] bytes = null;
        try {
            bytes = HTMLUtils.getVcode(path,cookie,referer);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return BitmapFactory.decodeByteArray(bytes,0,bytes.length);

    }




    //获取cookie
    public static String getCookie(String path) throws IOException{

        URL url=new URL(path);
        HttpURLConnection con=(HttpURLConnection)url.openConnection();
        con.setRequestMethod("GET");
        con.setDoInput(true);
        con.setDoOutput(true);
        InputStream in=con.getInputStream();
        Map<String, List<String>> map =con.getHeaderFields();
        List<String> rs =map.get("Set-Cookie");
//        String totalCookie="safedog-flow-item=;";
        String totalCookie = null;
        for(String s : rs) {
//            if(!s.startsWith("safedog-flow-item=; expires=Thur,")){
//                String subs = " "+s.substring(0,s.length()-" path=/".length());
//                totalCookie += subs;
//                System.out.println(totalCookie);
//            }
            if(s.startsWith("ASP.NET")){
                String subs = " "+s.substring(0,s.length()-" path=/".length());
                totalCookie = subs;
            }
        }
        in.close();
        con.disconnect();
        return totalCookie;
    }


    //获取验证码  字节数组。
    public static byte[] getVcode(String path,String Cookie,String referer) throws Exception {
        URL url=new URL(path);
        HttpURLConnection con=(HttpURLConnection)url.openConnection();
        con.setRequestMethod("GET");
        con.setDoInput(true);
        con.setDoOutput(true);
        con.addRequestProperty("Cookie", Cookie);
        con.setRequestProperty("Referer",referer);
        InputStream in=con.getInputStream();
        byte[] data = null;
        ByteArrayOutputStream output = new ByteArrayOutputStream();
        byte[] buf = new byte[1024];
        int numBytesRead = 0;
        while ((numBytesRead = in.read(buf)) != -1) {
            output.write(buf, 0, numBytesRead);
        }
        data = output.toByteArray();
        System.out.println(data.toString());
        output.close();
        in.close();
        con.disconnect();
        return data;

    }



    //获取StringBuffer
    public static StringBuffer getHTML(String path,String cookie,String referer,String params) throws Exception{
        StringBuffer buff = null;
        try {
            URL url=new URL(path);
            HttpURLConnection con =(HttpURLConnection)url.openConnection();
            con.setDoInput(true);
            con.setDoOutput(true);
            con.setRequestMethod("POST");
            con.setRequestProperty("Cookie", cookie);
            con.setRequestProperty("Referer", referer);
            OutputStream out=con.getOutputStream();
            out.write(params.getBytes());
            out.flush();
            buff = getStringBufferFromNet(con);
            out.close();
            con.disconnect();
        } catch (MalformedURLException e) {
            e.printStackTrace();
            System.out.println("输入路径错误");
        } catch (IOException e) {
            e.printStackTrace();
            System.out.println("httpURLConnection对象返回错误");
        }
        return buff;

    }


}
